(function(w){
	w.Calendar = function (opt) {
		// 创建日历控件基本结构	
		var cldbox = document.createElement("div");
		cldbox.className = 'calendar-container';
		var tpl = "";
		tpl += '<div class="calendar-title">';
		tpl += '<div class="calendar-prevyear"></div>';
		tpl += '<div class="calendar-prevmonth"></div>';
		tpl += '<div class="calendar-year"></div>';
		tpl += '<div class="calendar-month"></div>';
		tpl += '<div class="calendar-nextmonth"></div>';
		tpl += '</div>';
		tpl += '<div class="calendar-week"><div>日</div><div>一</div><div>二</div><div>三</div><div>四</div><div>五</div><div>六</div></div>';
		tpl += '<div class="calendar-content clearfix"></div>';
		cldbox.innerHTML=tpl;
		document.querySelector(opt.el).appendChild(cldbox);
		
		// dom 对象
		var omonth = cldbox.querySelector(".calendar-month");
		var oyear = cldbox.querySelector(".calendar-year");
		var prevmonth = cldbox.querySelector(".calendar-prevmonth"); //前一月
		var nextmonth = cldbox.querySelector(".calendar-nextmonth"); //后一月
		var content = cldbox.querySelector(".calendar-content"); //月份表格
		
		// 时间对象(默认当前)
		var dateObj;
		if(opt.value){
			dateObj = opt.value;
		}else{
			dateObj = new Date();
		}
		// 年月获取 
		var year = getYear(dateObj);
		var month = getMonth(dateObj);
		// 月年的显示
		omonth.innerHTML = month+"月";
		oyear.innerHTML = year+"年";
		// 获取本月1号的周值
		var fistWeek = getCurmonWeeknum(dateObj);
		// 本月总日数
		var monDaynum = getCurmonDaynum(dateObj);
		// 当前日期
		var nowDay = getDay(dateObj);
		//初始化显示本月信息
		setContent(content, fistWeek, monDaynum, nowDay, 0);
		
		//下一个月
		var cldbox = document.createElement("div");
		cldbox.className = 'calendar-container';
		var tpl = "";
		tpl += '<div class="calendar-title">';
		tpl += '<div class="calendar-prevyear"></div>';
		tpl += '<div class="calendar-prevmonth"></div>';
		tpl += '<div class="calendar-year"></div>';
		tpl += '<div class="calendar-month"></div>';
		tpl += '<div class="calendar-nextmonth"></div>';
		tpl += '</div>';
		tpl += '<div class="calendar-week"><div>日</div><div>一</div><div>二</div><div>三</div><div>四</div><div>五</div><div>六</div></div>';
		tpl += '<div class="calendar-content clearfix calendar-content1"></div>';
		cldbox.innerHTML=tpl;
		document.querySelector(opt.el).appendChild(cldbox);
		// dom 对象
		var omonth = cldbox.querySelector(".calendar-month");
		var oyear = cldbox.querySelector(".calendar-year");
		var prevmonth = cldbox.querySelector(".calendar-prevmonth"); //前一月
		var nextmonth = cldbox.querySelector(".calendar-nextmonth"); //后一月
		var content1 = cldbox.querySelector(".calendar-content1"); //月份表格
			var ddm = null;
			var ddy = null;
			if((dateObj.getMonth()+1)==12){
				ddm = 0;
				ddy = dateObj.getFullYear()+1;
			}else{
				ddm = dateObj.getMonth()+1;
				ddy = dateObj.getFullYear();
			};
			dateObj.setFullYear(ddy);
			//设置月份之前先判断下一个月有多少天,如果下个月天数比当前日期小，就把日期设置为下个月的天数	
			var nextMonthDay = getCurmonDaynum(dateObj,dateObj.getFullYear(),ddm);
			if(nextMonthDay < dateObj.getDate()){
				dateObj.setDate(nextMonthDay);
			}			
			dateObj.setMonth(ddm);
			omonth.innerHTML = getMonth(dateObj)+"月";
			oyear.innerHTML = dateObj.getFullYear()+"年";
		  	fistWeek = getCurmonWeeknum(dateObj);
		  	monDaynum = getCurmonDaynum(dateObj);
		  	nowDay = getDay(dateObj);
		  	setContent(content1, fistWeek, monDaynum, 0, 1);
		
		var isSupportMUI = (typeof mui === 'function');
		var evt = {
			type: isSupportMUI?'tap':'click'
		}
		// 显示当前时间
		content.addEventListener(evt.type,function (event) {
			var dayt,month;
			dateObj = new Date();
		    if(event.target.tagName.toLowerCase()=="div" && event.target.nodeType=="1" && hasclass(event.target.className,"canChoose") && !hasclass(event.target.className,"notap")){
		    		var day = event.target.innerHTML;
					dateObj = new Date(getYear(dateObj), getMonth(dateObj)-1, day);
					var week = getWeek(dateObj);	
						month = getMonth(dateObj);
						if(getMonth(dateObj)<10){
							month = '0'+ getMonth(dateObj);
						}
						dayt = day;
						if(day < 10){
							dayt = '0'+ day;
						}
				if(!hasclass(event.target.className,"canChoose-active")){
						addClass(event.target,"canChoose-active");
						Dateval = getYear(dateObj)+'-'+month+'-'+dayt;
						DateText = month+'月'+dayt+'日';
						DateArr.push(Dateval);
						DateTextArr.push(DateText);
				}else{
					Dateval = getYear(dateObj)+'-'+month+'-'+dayt;
					DateText = month+'月'+dayt+'日';
					removeClass(event.target,"canChoose-active");
					removeByValue(DateArr,Dateval);
					removeByValue(DateTextArr,DateText);
				}
		    };
		})
		
		content1.addEventListener(evt.type,function (event) {
			var dayt,month;
			dateObj = new Date();
		    if(event.target.tagName.toLowerCase()=="div" && event.target.nodeType=="1" && hasclass(event.target.className,"canChoose") && !hasclass(event.target.className,"notap")){
		    		var day = event.target.innerHTML;
					dateObj = new Date(getYear(dateObj), getMonth(dateObj), day);
					var week = getWeek(dateObj);	
						month = getMonth(dateObj);
						if(getMonth(dateObj)<10){
							month = '0'+ getMonth(dateObj);
						}
						dayt = day;
						if(day < 10){
							dayt = '0'+ day;
						}
				if(!hasclass(event.target.className,"canChoose-active")){
						addClass(event.target,"canChoose-active");
						Dateval = getYear(dateObj)+'-'+month+'-'+dayt;
						DateText = month+'月'+dayt+'日';
						DateArr.push(Dateval);
						DateTextArr.push(DateText);
				}else{
					Dateval = getYear(dateObj)+'-'+month+'-'+dayt;
					DateText = month+'月'+dayt+'日';
					removeClass(event.target,"canChoose-active");
					removeByValue(DateArr,Dateval);
					removeByValue(DateTextArr,DateText);
				}
		    };
		})
	}
	
	
	//有无指定类名的判断
	function hasclass(str,cla){
	  	var i=str.search(cla);
	  	if(i==-1){
	   		return false;
	  	}else{
	   		return true;
	  	};
	}
	
	//增加class属性
	function addClass(obj, cls){
    var obj_class = obj.className,//获取 class 内容.
        blank = (obj_class != '') ? ' ' : '';//判断获取到的 class 是否为空, 如果不为空在前面加个'空格'.
    added = obj_class + blank + cls;//组合原来的 class 和需要添加的 class.
    obj.className = added;//替换原来的 class.
	}
	
	//删除class属性
	function removeClass(obj, cls){
	    var obj_class = ' '+obj.className+' ';//获取 class 内容, 并在首尾各加一个空格. ex) 'abc    bcd' -> ' abc    bcd '
	    obj_class = obj_class.replace(/(\s+)/gi, ' '),//将多余的空字符替换成一个空格. ex) ' abc    bcd ' -> ' abc bcd '
	        removed = obj_class.replace(' '+cls+' ', ' ');//在原来的 class 替换掉首尾加了空格的 class. ex) ' abc bcd ' -> 'bcd '
	    removed = removed.replace(/(^\s+)|(\s+$)/g, '');//去掉首尾空格. ex) 'bcd ' -> 'bcd'
	    obj.className = removed;//替换原来的 class.
	}
	
	
	//根据元素值删除素组元素
	function removeByValue(arr, val) {
		  for(var i=0; i<arr.length; i++) {
		    if(arr[i] == val) {
		      arr.splice(i, 1);
		      break;
		    }
		  }
	}
	
	// 初始化日期显示方法
	function setContent(el, fistWeek, monDaynum, nowDay, buttonId){
		// 留空
		for(var i=1;i <= fistWeek;i++){
			var subContent = document.createElement("div");
			subContent.innerHTML = "";
			el.appendChild(subContent);
		}
		// 正常区域
		for(var i=1;i <= monDaynum;i++){
			var subContent = document.createElement("div");
			subContent.className="canChoose";
			if(buttonId == 0){
			   if(i< getDay(new Date())){
				 subContent.classList.add("notap");
			   }
			}
			if(buttonId == 1){
				if(i > sessionStorage.startDate){
				 subContent.classList.add("notap");
			   }
			}
			
			if(i==sessionStorage.startDate){
				subContent.classList.add("startDate");
			}
			if(sessionStorage.endDate< i &&  i<=sessionStorage.startDate){
				subContent.classList.add("notap");
			}
			if(i==sessionStorage.endDate){
				subContent.classList.add("endDate");
			}
			subContent.innerHTML = i;
			el.appendChild(subContent);
		}
	}

	// 清除内容
	function clearContent(el){
		el.innerHTML="";
	}
	
	// 判断闰年
	function isLeapYear(year){ 
		if( (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)){
			return true;
		}else{
			return false;
		}
	}
	
	// 得到当前年份
	function getYear (dateObj) {
		return dateObj.getFullYear()
	}
	
	// 得到当前月份
	function getMonth (dateObj) { 
		var month=dateObj.getMonth()
		switch(month) { 
			case 0: return "1"; break; 
		  	case 1: return "2"; break; 
		  	case 2: return "3"; break; 
		  	case 3: return "4"; break; 
		  	case 4: return "5"; break; 
		  	case 5: return "6"; break; 
		  	case 6: return "7"; break; 
		  	case 7: return "8"; break; 
		  	case 8: return "9"; break; 
		  	case 9: return "10"; break; 
		  	case 10: return "11"; break; 
		  	case 11: return "12"; break;   
		  	default: 
		}
	}
	
	// 得到当前号数
	function getDay (dateObj) {
		return dateObj.getDate();
	}
	
	// 得到周期数
	function getWeek (dateObj) {
		var week;
		switch (dateObj.getDay()){
		 	case 1: week = "星期一"; break;
		 	case 2: week = "星期二"; break;
		 	case 3: week = "星期三"; break;
		 	case 4: week = "星期四"; break;
		 	case 5: week = "星期五"; break;
		 	case 6: week = "星期六"; break;
		 	default: week = "星期天";
		}
		return week;
	}
	
	// 获取本月总日数方法
	//修改后做了判断，如果传入fullyear 和 curMonth 参数，就可以查询任意年份任一月份有多少天，用于处理31号的问题
	function getCurmonDaynum(dateObj,fullyear,curMonth) {
		var year = fullyear?fullyear:dateObj.getFullYear();
		var month = curMonth?curMonth:dateObj.getMonth();
		if(isLeapYear(year)){//闰年
			switch(month) { 
				case 0: return "31"; break; 
				case 1: return "29"; break; //2月
				case 2: return "31"; break; 
			   	case 3: return "30"; break; 
			   	case 4: return "31"; break; 
			   	case 5: return "30"; break; 
			   	case 6: return "31"; break; 
			   	case 7: return "31"; break; 
			   	case 8: return "30"; break; 
			   	case 9: return "31"; break; 
			   	case 10: return "30"; break; 
			   	case 11: return "31"; break;   
				default:  
			}
		}else{//平年
	   		switch(month) { 
	   			case 0: return "31"; break; 
	   			case 1: return "28"; break; //2月 
	   			case 2: return "31"; break; 
			   	case 3: return "30"; break; 
			   	case 4: return "31"; break; 
			   	case 5: return "30"; break; 
			   	case 6: return "31"; break; 
			   	case 7: return "31"; break; 
			   	case 8: return "30"; break; 
			   	case 9: return "31"; break; 
			   	case 10: return "30"; break; 
			   	case 11: return "31"; break;   
	   			default:  
			}   
		}
	}
	
	// 获取本月1号的周值
	function getCurmonWeeknum(dateObj) {
		var oneyear = new Date();
		var year = dateObj.getFullYear();
		var month = dateObj.getMonth(); //0是12月
		oneyear.setFullYear(year);
		//先设置1号，再改变月份，避免31号的问题
		oneyear.setDate(1);
		oneyear.setMonth(month); //0是12月
		return oneyear.getDay();
	}
})(window);
